package com.hk.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hk.reggie.common.CustomException;
import com.hk.reggie.entity.Dish;
import com.hk.reggie.entity.DishFlavor;
import com.hk.reggie.entity.Setmeal;
import com.hk.reggie.entity.SetmealDish;
import com.hk.reggie.mapper.DishMapper;
import com.hk.reggie.service.DishFlavorService;
import com.hk.reggie.service.DishService;
import com.hk.reggie.vo.DishVo;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;



    /**
     * 新增菜品的信息(包含忌口的信息)
     * @param dishVo
     */
    @Transactional
    public void saveDishInfo(DishVo dishVo) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishVo,dish);
        //保存菜品
        this.save(dish);

        //为口味设置菜品id
        Long dishId = dish.getId();
        List<DishFlavor> flavors = dishVo.getFlavors();
        List<DishFlavor> collect = flavors.stream().map((flavor) -> {
            flavor.setDishId(dishId);
            return flavor;
        }).collect(Collectors.toList());

        //保存口味
        dishFlavorService.saveBatch(collect);
    }

    public DishVo getByIdWithFlavor(Long id) {
        //查询菜品基本信息
        Dish dish = this.getById(id);
        DishVo dishVo = new DishVo();

        //属性对拷
        BeanUtils.copyProperties(dish,dishVo);

        //查询口味信息
        List<DishFlavor> list = dishFlavorService.list(new QueryWrapper<DishFlavor>().eq("dish_id", dishVo.getId()));

        //设置口味信息
        dishVo.setFlavors(list);
        return dishVo;
    }

    @Override
    @Transactional
    public void updateWithFlavor(DishVo dishVo) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishVo,dish);

        Long dishId = dish.getId();

        //更新菜品
        this.updateById(dish);

        //清理当前菜品对应口味数据
        dishFlavorService.remove(new QueryWrapper<DishFlavor>().eq("dish_id", dishId));

        //添加当前菜品对应口味数据
        List<DishFlavor> flavors = dishVo.getFlavors();
        List<DishFlavor> collect = flavors.stream().map((flavor) -> {
            flavor.setDishId(dishId);
            return flavor;
        }).collect(Collectors.toList());

        //保存口味
        dishFlavorService.saveBatch(collect);

    }


    public void removeWithDish(List<Long> ids) {
        //查询是否是停售状态
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids).eq("status", 1);
        int count = this.count(queryWrapper);
        if(count > 0){
            //如果不能删除则抛出一个业务异常
            throw new CustomException("菜品正在售卖中，不能删除");
        }


        //先删除套餐表
        this.removeByIds(ids);

        //再删除关系表
       dishFlavorService.remove(new QueryWrapper<DishFlavor>().in("dish_id", ids));
    }


}
